package com.controller;


import com.DTO.FriendDTO;
import com.Exception.NormalException.FriendExceptions.FriendAlreadyExistException;
import com.Exception.NormalException.NormalException;
import com.Result.FriendApplyResult;
import com.WebSocket.SocketData;
import com.WebSocket.WebSocket;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.FriendApply;
import com.service.FriendApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/friendApply")
@CrossOrigin
@Api(tags = "好友申请管理器")
public class FriendApplyController {
    @Autowired
    private FriendApplyService friendApplyService;

    @PostMapping("/addApply")
    @ApiOperation(value = "添加好友申请")
    public boolean addApply(@RequestBody FriendDTO friendDTO) {
        FriendApply friendApply = new FriendApply();
        BeanUtils.copyProperties(friendDTO, friendApply);
        friendApply.setApplyTime(new Date());
        friendApplyService.save(friendApply);
        return friendApplyService.sendFriendApply(friendApply);
    }

    @GetMapping("/acceptApply")
    @ApiOperation(value = "同意某条好友申请")
    @ApiResponses({
            @ApiResponse(code = FriendAlreadyExistException.code, message = FriendAlreadyExistException.message)
    })
    public boolean acceptApply(Integer applyId, HttpServletResponse response) {
        try {
            this.friendApplyService.acceptApply(applyId);
            this.friendApplyService.sendApplyResult(applyId, true);
            return true;
        } catch (NormalException e) {
            e.setStatus(response);
            return false;
        }
    }

    @GetMapping("/refuseApply")
    @ApiOperation(value = "拒绝好友申请")
    public boolean refuseApply(Integer applyId) {
        this.friendApplyService.refuseApply(applyId);
        this.friendApplyService.sendApplyResult(applyId, false);
        return true;
    }

    @GetMapping("/getByUserID")
    @ApiOperation(value = "获取某用户的好友申请")
    public List<FriendApplyResult> getAllWaited(Integer userId) {
        return this.friendApplyService.getByUserId(userId);
    }

    @GetMapping("/getAll")
    public List<FriendApply> getAll() {
        return this.friendApplyService.list();
    }

}

